// pages/invoiceRecord/invoiceRecord.js
var that,timer,
  api = new (require('../../utils/api.js')),
  headimg = api.getimgurl(),
  app = getApp(),
  qrcode = require('../../utils/qrcode.js'),
  util = new (require('../../utils/util.js'));
Page({

  /**
   * 页面的初始数据
   */
  data: {
    ischeck:2,
    orderlist:[
    ],
    page:1,
    showmodal:!1,
    stationId:0,
    isload:!1,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    that=this;
    that.setData({stationId:options.stationId,ischeck:options.cateId},()=>{
      that.getdata()
    })
  },
  getdata(){
    let {ischeck,orderlist,page,stationId}=that.data
    api.invoicerecord({stationId,cateId:ischeck,page},res=>{
      if(res.data.code==0){
        let {nextPage,recordArr}=res.data.entity.data
        if(recordArr.length>0){
          recordArr.forEach(x=>{
            x.date=util.formatTime(new Date(x.addTime*1000))
            orderlist.push(x)
          })
        }
        that.setData({nextPage,orderlist,isload:!0})
      }
    })

  },
  tosel(e){
    let {index} = e.currentTarget.dataset,{ischeck} = that.data
    if(index==ischeck){
      return
    }
    that.setData({ischeck:index,orderlist:[],page:1},()=>{
      that.getdata()
    })
  },
   // 弹出二维码
   showqrstr(e){
     let {qrstr}=e.currentTarget.dataset
      that.setData({showmodal:!0,qrstr},()=>{
        that.getSystemInfo()
      })     
   },
   getSystemInfo(){
    wx.showLoading({
      title: '加载中',
    })
    wx.getSystemInfo({
      success: (res) => {
        new qrcode({
          width: res.screenWidth * 0.515,
          height: res.screenWidth * 0.515,
          canvasId: 'canvas',
          text: that.data.qrstr,
          callback: res => {
            wx.hideLoading();
            that.getisDeal()
          }
        })
      },
    })
  },
  getisDeal(){
    let {qrstr,stationId}=that.data
    timer=setInterval(function () {
      api.invoicedealstatus(qrstr,res=>{
        if(res.data.code==0){
          clearInterval(timer);
          if(res.data.entity.data.isDeal==1){
            // console.log(1111);
            that.setData({showmodal:!1},()=>{
              wx.redirectTo({
                url: '../InvoiceSucc/InvoiceSucc?stationId='+stationId,
              })
            })
          }
        }else{
          clearInterval(timer);
          that.setData({showmodal:!1})
          wx.showToast({
            title: res.data.message,
            icon: 'none'
          })
        }
      })
    },3000)
    that.setData({timer})
  },
  closemodal(){
    that.setData({showmodal:!1},()=>{
      clearInterval(that.data.timer);
      // that.getdata()
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let {isload}=that.data
    if(isload){
      that.setData({orderlist:[],page:1,},()=>{
        that.getdata()
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    clearInterval(timer);
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    clearInterval(timer);
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let {nextPage,page}=that.data
    if(nextPage==1){
      page=page+1
      that.setData({page},()=>{
        that.getdata()
      })
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})